/*
-----------------------------------------------------------------------------
Filename:    TutorialApplication.cpp
-----------------------------------------------------------------------------

This source file is part of the
   ___                 __    __ _ _    _ 
  /___\__ _ _ __ ___  / / /\ \ (_) | _(_)
 //  // _` | '__/ _ \ \ \/  \/ / | |/ / |
/ \_// (_| | | |  __/  \  /\  /| |   <| |
\___/ \__, |_|  \___|   \/  \/ |_|_|\_\_|
      |___/                              
      Tutorial Framework
      http://www.ogre3d.org/tikiwiki/
-----------------------------------------------------------------------------
*/
#include "TutorialApplication.h"
#include <OgreTerrain.h>
#include <OgreTerrainGroup.h>


//-------------------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void)
{
	
	
}
//-------------------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void)
{
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createScene(void)
{
	/*
	Example for create plane
	*/

	Ogre::Light* light =mSceneMgr->createLight("DirectionLight");
	light->setType(Ogre::Light::LT_DIRECTIONAL);
	light->setDiffuseColour(Ogre::ColourValue(1.0f,1.0f,1.0f,1.0f));
	light->setDirection(Ogre::Vector3(1,-1,0));
	
	ogreEnt= mSceneMgr->createEntity("Sinbad", "Sinbad.mesh");
	ogreNode= mSceneMgr->getRootSceneNode()->createChildSceneNode("NodeOgre");
	ogreNode->setPosition(0,3.2f,0);
	ogreNode->scale(3.0f,3.0f,3.0f);
	ogreNode->attachObject(ogreEnt);

	_anState = ogreEnt->getAnimationState("Dance");
	_anState->setEnabled(true);
	_anState->setLoop(true);

	//add two sword to Sinbad
	Ogre::Entity* sword1 = mSceneMgr->createEntity("sword1","Sword.mesh");
	ogreEnt->attachObjectToBone("Handle.L",sword1);
	
		
	Ogre::Plane plane(Ogre::Vector3::UNIT_Y, -10);
	Ogre::MeshManager::getSingleton().createPlane("plane",Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
            1500,1500,200,200,true,1,5,5,Ogre::Vector3::UNIT_Z);
	Ogre::Entity* ent = mSceneMgr->createEntity("LightPlaneEntity","plane");
	Ogre::SceneNode* planeNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("planeNode");
	planeNode->attachObject(ent);
	ent->setMaterialName("Examples/BeachStones");

	mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
		
}

void TutorialApplication::createFrameListener(void)
{
	Ogre::FrameListener* FrameListener = new mFrameListener(ogreNode,ogreEnt,mWindow,mCamera);
	mRoot->addFrameListener(FrameListener);

}



#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        TutorialApplication app;

        try {
            app.go();
        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32			
			//MessageBox(NULL,e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);						
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }

        return 0;
    }

#ifdef __cplusplus
}
#endif
